using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DTO;
using BUS;
namespace Presentation
{
    public partial class EmployeeGUI : Form
    {
        public EmployeeGUI()
        {
            InitializeComponent();
        }

        public void ShowMaster()
        {
            this.dgvEmployee.AutoGenerateColumns = false;
            int employeeStyle = int.Parse(cbEmployeeStyle.SelectedValue.ToString());
            this.dgvEmployee.DataSource = EmployeeBUS.SelectEmployeeByEmployeeStyle(employeeStyle);
         }

        private void EmployeeForm_Load(object sender, EventArgs e)
        {
            this.cbEmployeeStyle.ValueMember = "EmployeeStyleID";
            this.cbEmployeeStyle.DisplayMember = "EmployeeStyleName";
            this.cbEmployeeStyle.DataSource = EmployeeStyleBUS.SelectEmployeeStyleAll();

            this.ShowMaster();          
        }

        private void cbEmployeeStyle_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.ShowMaster();  
        }

        private void dgvEmployee_SelectionChanged(object sender, EventArgs e)
        {

            if (this.dgvEmployee.CurrentRow != null)
            {
                this.txtEmployeeID.Text = this.dgvEmployee.CurrentRow.Cells["EmployeeID"].Value.ToString();
                this.txtName.Text = this.dgvEmployee.CurrentRow.Cells["EmployeeName"].Value.ToString();
                this.txtEmail.Text = this.dgvEmployee.CurrentRow.Cells["Email"].Value.ToString();
                this.txtSalary.Text = this.dgvEmployee.CurrentRow.Cells["Salary"].Value.ToString();              
            }

        }

        private void btReset_Click(object sender, EventArgs e)
        {
            this.txtEmployeeID.Text = "";
            this.txtName.Text = "";
            this.txtEmail.Text = "";
            this.txtSalary.Text = "";               
        }

        private void btInsert_Click(object sender, EventArgs e)
        {
            EmployeeDTO emp = new EmployeeDTO();

            emp.EmployeeID = this.txtEmployeeID.Text;
            emp.Name = this.txtName.Text;
            emp.Email = this.txtEmail.Text ;
            emp.Salary =float.Parse (this.txtSalary.Text);
            emp.EmployeeStyle = int.Parse(this.cbEmployeeStyle.SelectedValue.ToString());
            bool result = EmployeeBUS.InsertEmployee(emp);
            if (result == true)
            {
                MessageBox.Show("Successful!", "Insert", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.ShowMaster();
            }
            else
            {
                MessageBox.Show("Fail!\r\nChoose new EmployeeID,Please!", "Insert", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btUpdate_Click(object sender, EventArgs e)
        {
            EmployeeDTO emp = new EmployeeDTO();

            emp.EmployeeID = this.txtEmployeeID.Text;
            emp.Name = this.txtName.Text;
            emp.Email = this.txtEmail.Text;
            emp.Salary = float.Parse(this.txtSalary.Text);
            emp.EmployeeStyle = int.Parse(this.cbEmployeeStyle.SelectedValue.ToString());
            bool result = EmployeeBUS.UpdateEmployee(emp);
            if (result == true)
            {
                MessageBox.Show("Successful!", "Update", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.ShowMaster();
            }
            else
            {
                MessageBox.Show("Fail!\r\nCheck EmployeeID,Please!", "Update", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btDelete_Click(object sender, EventArgs e)
        {
            if (this.dgvEmployee.CurrentRow != null)
            {
                String employeeID = this.dgvEmployee.CurrentRow.Cells["EmployeeID"].Value.ToString();
                DialogResult dr = MessageBox.Show("Do you want to delete EmployeeID " + employeeID + "?", "Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
                if (dr == DialogResult.Yes)
                {
                    bool result = EmployeeBUS.DeleteEmployeeByID(employeeID);
                    if (result == true)
                    {
                        MessageBox.Show("Successful!", "Delete", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        this.ShowMaster();
                    }
                    else
                    {
                        MessageBox.Show("Fail!\r\nCheck EmployeeID,Please!", "Delete", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
            }
            else
            {
                MessageBox.Show("Fail!\r\nChoose one employee,Please!", "Delete", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btExit_Click(object sender, EventArgs e)
        {
            DialogResult dr = MessageBox.Show("Do you want to exit?", "Exit", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
            if (dr == DialogResult.Yes)
            {
                this.Close ();
            }
        }
    }
}

